﻿using System.Net;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using wne.core;
using wne.core.Interface;
using wne.models;

namespace wne.Controllers
{
    public class AccountController : Controller
    {
        private AccountFacade facade;
        private AdminFacade facade_admin;
        public AccountController()
        {
            facade = new AccountFacadeSql();
        }
        public ActionResult Login()
        {
            var logOnModel = new LogOnModel();
            ViewBag.IsInvalide = false;
            return View(logOnModel);
        } 

        [HttpPost]
        public ActionResult Login(LogOnModel model)
        {
            var facade = new AccountFacadeSql();
            if (facade.IsValidUserPass(model.UserName, model.Password))
            {
                FormsAuthentication.SetAuthCookie(model.UserName, false);
            }
            //Костыли и велосипеды!
            Request.Cookies.Remove("admin");
            Response.Cookies.Remove("admin");
            if (facade.IsAdmin(model.UserName))
                Response.Cookies.Add(new HttpCookie("admin","true"));
            else
            {
                Response.Cookies.Add(new HttpCookie("admin", "false"));
            }
            return Redirect(model.ReturnUrl ?? "/");
        }

        public ActionResult Logoff()
        {
            Response.Cookies.Remove("admin");
            FormsAuthentication.SignOut();
            return Redirect(FormsAuthentication.LoginUrl);
        }
        

    }
}

//namespace wne.Controllers
//{
        //    Request.Cookies.Remove("auth");
        //    Request.Cookies.Remove("admin");
        //    if (wne.core.Class1.IsValidLogOn(model.UserName, model.Password)) //(wne.core.Class1.IsValidUserLogOn(model.UserName, model.Password))
//    {
        //        HttpCookie logIn = new HttpCookie("auth", model.UserName);
        //        Response.Cookies.Add(logIn);
        //        if (wne.core.Class1.IsAdmin(model.UserName)) //wne.core.Class1.IsAdmin(model.UserName)
//        {
        //            HttpCookie admin = new HttpCookie("admin", model.UserName);
        //            Response.Cookies.Add(admin);
//        }
        //        if (!returnUrl.Equals(""))
        //            return Redirect(returnUrl);
        //        return Redirect("/Home/Index");
        //    }
        //    ViewBag.IsInvalide = true;
        //    var logOnModel = new LogOnModel();
        //    return View(logOnModel);
        //}
        
        //public ActionResult LogOff()
        //{
        //    Request.Cookies.Remove("auth");
        //    Request.Cookies.Remove("admin");
        //    return Redirect("/Account/LogOn");
        //}
        //public ActionResult PLoginBox()
        //{
        //    //HttpCookie cookie = HttpContext.Current.Response.Cookies[cookieName];
        //    //if ()
        //    if (Request.Cookies["auth"] != null)
        //    {
        //        ViewBag.Auth = true;
        //        ViewBag.UserName = Request.Cookies["auth"].Value;
        //    }
        //    else
        //    {
        //        ViewBag.Auth = false;
//    }
        //    return View();
//}


    